Regular expressions are an incredibly
useful tool for processing text data. They allow you to create
a search pattern that actually matches
many different text strings. Thus instead of having to write
out each and every word and phrase with all the types of
capitalization and punctuation, you can instead write a single regular
expression to find them all at once. Regular expressions or regexes
reads text from left to right. If it matches the first character, then it evaluates the second character. If it doesn't match, then it starts over, moves one character to the right and
re-evaluates the first character. This continues until all characters of
the regular expression are matched. Part of the reason that
regular expressions are so flexible is that you can use wildcards and character classes to match any one of many characters at once. In regular expression, the period is a wildcard that can match
any single character. Be it a letter, a number or whitespace. Things like spaces, tabs, and new lines. Character classes are collections of characters that can match any one of
the values in the character class. For example, if you want to match
any letter in the alphabet, you can use brackets to indicate
any letter A through Z. Note that this is case-sensitive. You can do the same thing for
capital A through capital Z, and if you want to match
any letter regardless of the case, you can combine these
into a single bracket. If you don't want to match
the characters in a bracket, you can put a curly at the beginning
of the character class. In some cases, there are popular groupings that actually
get a character class shorthand. For example, backslash W matches
all alphanumeric characters. Well, backslash D matches all digits. There are also character classes
that exclude types of matches. For example, backslash W matches
all non alphanumeric characters. You can also use quantifiers after
the character or character class to say how many times you want to match that particular character
or character class. For example, if you want
it to match at least once, you can use the plus sign. If you want it to match exactly twice, you can use curly brace too. Use the same notation for matching
anywhere between two and four times. Finally, if the character or
character classes option, you can use the asterisks
to map zero or more times. If you wanted to only
match zero or one times, you can use a question mark. If you want to allow for
matching one value or another, you can use a vertical bar to alternate. The bar matches either the character
before the bar or after the bar. You can also use parentheses
to group values with your whole statement if you want to
choose between groups of characters. Parentheses also allow you
to create capturing groups. Capturing groups basically
save everything that matches the regular expression
in the parenthesis. You can then extract this information
if you want to in another command. Other searches you may want
to do may require you to only look for matches at the beginning
or the end of the string. This is called anchoring. The curly can be used at the beginning of the regular expression to only match the beginning of the input while the dollar sign will only
match the end of the input. Regular expressions also have
modifiers that can apply. The G modifier is the global command. It will keep searching
the tax and find all matches rather than just stopping after the regular expression
finds the first match. The I modify our makes
the regular expression case insensitive, meaning that it will match
uppercase and lowercase letters. The actual software driving and executing these regular expressions
are called regex engines. Two of the most common
regex engines are PCRE, Perl Compatible Regular Expressions
and JavaScript regular expressions. Unfortunately, these two
regex engines each have their own rules and have
slightly different character classes. Packages and commands in "I" use
a variety of regular expression engines. In general, when a regular expression
isn't working as you think it should, make sure to check
which engine you are using. I can't tell you how much time I
have lost trying to figure out why my awesome regular expression wasn't working only to find out that it was
written using a different engine. At this point in the video, your head is probably spinning and how much content we have
covered so quickly. That's okay and totally expected. Regular expressions are extremely
complicated and really require you to just practice applying them to truly get to know them inside and out. The good news is, that there are a number of resources on the web to help you learn
regular expressions. Two of my favorite web tools
are RegExr and Debuggex. Both tools support PCRE and
JavaScript regular expressions. They allow you to copy and paste your own text to use while
building your regular expressions. However, they have
different ways of displaying information about the regular expression
that you may find helpful. Let's take this example. I wanted to build a regular expression
that can match any of these phrases. In RegExr, I can see that
the regular expression I built does match all of the phrases
because they all turn blue. Down in the tool section, I can see more details about each
component of the regular expression. On the left side, are a number of references and tools you can use to help
develop your RegExr. Overall, I think that
this is the best tool for checking what does and
does not match a RegExr. However, I love Debuggex for
understanding and building your regular expression
because it actually diagrams the logic of
the regular expression. Let's take the exact same RegExr. As we can see at group 1, there's the option to just skip
the whole part if necessary. So, the first phrase we wanted
to match physical exam colon, matches the first part then skips group 1 and group 2 before matching
the colon and completing. If we change the regular
expression to match group 1, zero or more times, now there is also a loop. That means we can match the nation
as many times as we want. We will link to these
resources in the radiance and give you lots of examples
to help you get started. With some practice and hard work, I have no doubt that you'll be able to apply regular expressions in no time.